package com.ktg.mes.md.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ktg.common.core.domain.APIResult;
import com.ktg.common.exception.ServiceException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.md.domain.MdLabelPrint;
import com.ktg.mes.md.service.IMdLabelPrintService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 产品标签打印Controller
 *
 * @author byteco
 * @date 2022-12-08
 */
@RestController
@RequestMapping("/md/mdLabelPrint")
public class MdLabelPrintController extends BaseController
{
    @Autowired
    private IMdLabelPrintService mdLabelPrintService;

    /**
     * 根据物料id 查询标签打印 详情信息
     * @param itemId
     * @return
     */
    @GetMapping("/ByItemId/{itemId}")
    public Result<MdLabelPrint> getByItemId(@PathVariable Long itemId,HttpServletRequest req){
        APIResult apiLog = new APIResult("查询产品标签打印列表",req,null);
        LambdaQueryWrapper<MdLabelPrint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MdLabelPrint::getItemId,itemId);
        wrapper.last("limit 1 ");
        try {
            MdLabelPrint print = mdLabelPrintService.getOne(wrapper);
            return apiLog.ok(print);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 查询产品标签打印列表
     */
    @PreAuthorize("@ss.hasPermi('md:mdLabelPrint:list')")
    @GetMapping("/list")
    public Result<IPage<MdLabelPrint>> list(MdLabelPrint mdLabelPrint,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询产品标签打印列表",req,null);
        try {
            Page<MdLabelPrint> page = new Page<MdLabelPrint>(pageNo, pageSize);
            IPage<MdLabelPrint> pageList = mdLabelPrintService.queryPageList(mdLabelPrint,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出产品标签打印列表
     */
    @PreAuthorize("@ss.hasPermi('md:mdLabelPrint:export')")
    @Log(title = "产品标签打印", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdLabelPrint mdLabelPrint,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出产品标签打印列表",req,mdLabelPrint);
        try {
            List<MdLabelPrint> list = mdLabelPrintService.queryList(mdLabelPrint);
            ExcelUtil<MdLabelPrint> util = new ExcelUtil<MdLabelPrint>(MdLabelPrint.class);
            util.exportExcel(response, list, "产品标签打印数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取产品标签打印详细信息
     */
    @PreAuthorize("@ss.hasPermi('md:mdLabelPrint:query')")
    @GetMapping(value = "/{id}")
    public Result<MdLabelPrint> getInfo(@PathVariable("id") Long id,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取产品标签打印详细信息",req,null);
        try {
            return apiLog.ok(mdLabelPrintService.getInfo(id));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增产品标签打印
     */
    @PreAuthorize("@ss.hasPermi('md:mdLabelPrint:add')")
    @Log(title = "产品标签打印", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<String> add(@RequestBody MdLabelPrint mdLabelPrint,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增产品标签打印",req,mdLabelPrint);
        MdLabelPrint print = mdLabelPrintService.getOne(new LambdaQueryWrapper<MdLabelPrint>()
                .eq(MdLabelPrint::getBatchNo, mdLabelPrint.getBatchNo()));
        if(ObjectUtils.isNotNull(print)){
            throw new ServiceException("生产批次号不能重复");
        }
        try {
            mdLabelPrintService.insert(mdLabelPrint);
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改产品标签打印
     */
    @PreAuthorize("@ss.hasPermi('md:mdLabelPrint:edit')")
    @Log(title = "产品标签打印", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody MdLabelPrint mdLabelPrint,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改产品标签打印",req,mdLabelPrint);
        try {
            mdLabelPrintService.update(mdLabelPrint);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除产品标签打印
     */
    @PreAuthorize("@ss.hasPermi('md:mdLabelPrint:remove')")
    @Log(title = "产品标签打印", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result<String> remove(@PathVariable Long[] ids,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除产品标签打印",req,null);
        try {
            mdLabelPrintService.delete(ids);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
